//
//  AbstractBaseViewController.m
//  MISCloud
//
//  Created by goldeney on 13-8-16.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "AbstractBaseViewController.h"
#import "IQKeyboardManager.h"

@interface AbstractBaseViewController ()

@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *subLable;

@end

@implementation AbstractBaseViewController
@synthesize backResultDelagate = _backResultDelagate;
@synthesize key = _key;
@synthesize sender = _sender;
@synthesize value = _value;
@synthesize tapGr;
@synthesize swipeGr;

- (id)initWithValue:(id)object
{
    self = [super init];
    if (self) {
        self.value = object;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        UIImage *backImage = [UIImage imageNamed:@"skin_header_btn_back_normal.png"];
        UIImage *highlightImage = [UIImage imageNamed:@"skin_header_btn_back_press.png"];
        
        CGRect backframe = CGRectMake(0,0,backImage.size.width,44);
        UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
        [backButton setImage:backImage forState:UIControlStateNormal];
        [backButton setImage:highlightImage forState:UIControlStateHighlighted];
        [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    }
    return self;
}

- (void)setValue:(id)object
{
    _value = object;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [[IQKeyboardManager sharedManager] setShouldToolbarUsesTextFieldTintColor:YES];
    
    tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
    
    swipeGr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    swipeGr.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeGr];
    
    [self getPassingParameter:self.value];
}

- (void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    IQKeyboardManager *mKeyboard = [IQKeyboardManager sharedManager];
    [mKeyboard resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar lt_setBackgroundColor:ThemeColorBlue];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                [UIColor whiteColor], NSForegroundColorAttributeName,
                                                [UIFont boldSystemFontOfSize:18.0], NSFontAttributeName, nil]];
}

#pragma mark 设置副标题

- (void)setTitle:(NSString *)title subtitle:(NSString*)subtitle
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    self.navigationItem.titleView = view;
    
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(-120, 0, 240, 30)];
    self.titleLable.textColor = [UIColor whiteColor];
    self.titleLable.textAlignment = NSTextAlignmentCenter;
    self.titleLable.font = [UIFont boldSystemFontOfSize:18.0];
    self.titleLable.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.12];
    self.titleLable.shadowOffset = CGSizeMake(0, -1);
    self.titleLable.text = title;
    [view addSubview:self.titleLable];
    
    self.subLable = [[UILabel alloc] initWithFrame:CGRectMake(-120, 26, 240, 14)];
    self.subLable.textColor = [UIColor whiteColor];
    self.subLable.textAlignment = NSTextAlignmentCenter;
    self.subLable.font = [UIFont boldSystemFontOfSize:12.0];
    self.subLable.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.12];
    self.subLable.shadowOffset = CGSizeMake(0, -1);
    self.subLable.text = subtitle;
    [view addSubview:self.subLable];
}

- (void)setSubtitle:(NSString*)subtitle
{
    if(!self.subLable) return;
    self.subLable.text = subtitle;
}

#pragma mark -
#pragma mark 键盘管理
-(void)enableKeyboardManger
{
    //键盘设置有效
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

-(void)disableKeyboardManager
{
    //键盘设置无效
    [[IQKeyboardManager sharedManager] setEnable:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    if (tapGr) [self.view removeGestureRecognizer:tapGr];
    if (swipeGr) [self.view removeGestureRecognizer:swipeGr];
    
    tapGr = nil;
    swipeGr = nil;
    
    _backResultDelagate = nil;
    _key = nil;
    _sender = nil;
    
    _value = nil;
}

#pragma mark -
#pragma mark 返回数据事件方法
- (void)getPassingParameter:(id)object
{
    
}

#pragma mark -
#pragma mark JGProgressHUD 普通型

- (JGProgressHUD *)showProgressHUDText:(NSString*)text {
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.tag = 1;
    HUD.delegate = self;
    
    HUD.textLabel.text = @"成功";
    HUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    HUD.square = YES;
    
    // 延迟0.5秒执行：
    [HUD showInView:self.navigationController.view];
    [HUD dismissAfterDelay:1.0];
    
    return HUD;
}

#pragma mark -
#pragma mark JGProgressHUD 定制型

- (JGProgressHUD *)showSimpleHUDText:(NSString*)text {
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = @"加载中...";
    HUD.delegate = self;
    HUD.tag = 0;
    
    if(text && text.length>0)
    HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    
    [HUD showInView:self.navigationController.view];
    //[HUD dismissAfterDelay:3.0];
    return HUD;
}

- (JGProgressHUD *)showErrorHUDText:(NSString*)text
{
    return [self showErrorHUDText:text dismissAfterDelay:0.6f];
}

- (JGProgressHUD *)showSuccessHUDText:(NSString*)text
{
    return [self showSuccessHUDText:text dismissAfterDelay:0.6f];
}

- (JGProgressHUD *)showSuccessHUDText:(NSString*)text dismissAfterDelay:(CGFloat)delay
{
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.tag = 1;
    HUD.delegate = self;
    
    HUD.textLabel.text = @"成功";
    HUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    HUD.square = YES;
    
    // 延迟0.5秒执行：
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
            [HUD showInView:self.navigationController.view];
            [HUD dismissAfterDelay:1.0];
        });
    });
    
    return HUD;
}

- (JGProgressHUD *)showErrorHUDText:(NSString*)text  dismissAfterDelay:(CGFloat)delay
{
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.tag = 2;
    HUD.delegate = self;
    
    HUD.textLabel.text = @"失败";
    HUD.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    HUD.square = YES;
    
    // 延迟0.5秒执行：
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
            [HUD showInView:self.navigationController.view];
            [HUD dismissAfterDelay:1.0];
        });
    });
    
    return HUD;
}

#pragma mark -
#pragma mark 返回数据事件方法

- (void) setTagKey:(id)keyValue
{
    self.key = keyValue;
}

- (void) setTagSender:(id)senderValue
{
    self.sender = senderValue;
}

#pragma mark -
#pragma mark BackResultDataDelagate

- (void) backResultDataKey:(id)key dataValue:(id)backValue
{
    
}

- (void) backResultDataKey:(id)keyValue sender:(id)senderValue dataValue:(id)backValue
{
    
}

#pragma mark -
#pragma mark IBAction Event
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end


